import CryptoJS from 'crypto-js'

export default {
  // 随机生成指定数量的16进制key
  generatekey (num) {
    let library = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
    let key = ''
    for (var i = 0; i < num; i++) {
      let randomPoz = Math.floor(Math.random() * library.length)
      key += library.substring(randomPoz, randomPoz + 1)
    }
    return key
  },

  // 加密
  encrypt (word, keyStr) {
    var result = ''
    try {
      keyStr = keyStr ? keyStr : 'TMOMP20190422ASE'// 判断是否存在ksy，不存在就用定义好的key
      var key = CryptoJS.enc.Utf8.parse(keyStr)
      var srcs = CryptoJS.enc.Utf8.parse(word)
      var encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
      result = encrypted.toString()
    } catch (e) {
      if (typeof word == 'string') {
        result = word
      } else {
        result = JSON.stringify(word)
      }
    }
    return result
  },
  // 解密
  decrypt (word, keyStr) {
    var result = ''
    try {
      if (typeof word == 'string') {
        keyStr = keyStr ? keyStr : 'TMOMP20190422ASE'
        var key = CryptoJS.enc.Utf8.parse(keyStr)
        var decrypt = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
        result = CryptoJS.enc.Utf8.stringify(decrypt).toString()
      } else {
        result = JSON.stringify(word)
      }

    } catch (e) {
      if (typeof word == 'string') {
        result = word
      } else {
        result = JSON.stringify(word)
      }
    }
    return result
  }

}
